﻿using System;
using CardLogistics.Model.Basic;
using CardLogistics.Web.Components;
using System.Data;
using CardLogistics.DAL;
using System.Text.RegularExpressions;

namespace CardLogistics.Web.MainPage.Basic.SysSuppliers
{
    public partial class AddEdit : BasePage
    {
        private static readonly CardLogistics.DAL.Basic.Basic_SuppliersData DAL = new CardLogistics.DAL.Basic.Basic_SuppliersData();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public void InitControl()
        {
            if (IsEdit)
            {
                string _key = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(_key))
                {
                    Basic_Suppliers sData = DAL.GetModel(_key);
                    SetWebControls(this.Page, sData);
                }
            }
            //else
            //{
            //    SUPPLIERSNO.Value = GetMAXNO("BASIC_SUPPLIERS", "SUPPLIERSNO", " and ISDELETE=0 ");
            //}
        }
        /// <summary>
        /// 保存按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            try
            {
                string result = "";
                if (IsEdit)
                {
                    result = DAL.Update(EncapsulationModel());
                }
                else
                    result = DAL.Add(EncapsulationModel());
                if (result.Split(',')[0] == "ok")
                {
                    ReturnListInfo();
                }
                else
                {
                    ShowMessageBox("操作失败请重新操作！" + result.Split(',')[1]);
                }
            }
            catch (Exception ex)
            {
                ShowMessageBox("系统异常:" + ex.ToString());
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private Basic_Suppliers EncapsulationModel()
        {
            Basic_Suppliers model = new Basic_Suppliers();
            model = GetWebControls<Basic_Suppliers>(this.Page);
            model.LASTUPDATEUSER = LoggedUser.ID;
            if (model.STATUS == null)
                model.STATUS = 0;
            model.ISDELETE = 0;
            if (IsEdit)
                model.SUPPLIERSID = Request.QueryString["id"];
            return model;
        }
    }
}